const {EventEmitter} = require("events")

class Dog extends EventEmitter {
    constructor(name, energy) {
        super()
        this.#name = name
        this.#energy = energy
        this.#start()
    }

    get name() {return this.#name}
    get energy() {return this.#energy}
    set energy(e) {
        this.#energy = e
        this.#start()
    }

    #name = ''
    #energy = 0
    #timer  = null
    #start() {
        if(this.#timer !== null) return
        
        this.#timer = setInterval(()=> {
            if(this.#energy > 0) {
              this.emit('bark');
              this.#energy--;
            } else {
              clearInterval(this.#timer);
              this.#timer = null;
            }
          }, 1000);
    }
}

module.exports = Dog